{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello \" Python\n",
      "Hello \" Python\n",
      "\n",
      "Hello\n",
      "    Python  \n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello \\\" Python\")\n",
    "\n",
    "print('Hello \" Python')\n",
    "\n",
    "print('''\n",
    "Hello\n",
    "    Python  \n",
    "'''\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = 123\n",
    "y = 456\n",
    "print(f\"{x + y} 是变量 x + y 的和\")\n",
    "\n",
    "# 字符串匹配\n",
    "print(\"xy\" in \"123xy456\")\n",
    "\n",
    "print(\"x\" not in \"123x456\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串连接\n",
    "x = \"abc\"\n",
    "y = \"xyz\"\n",
    "print(x + y)\n",
    "print(x * 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串切片运算\n",
    "s = \"abcde\"\n",
    "print(s[0])\n",
    "\n",
    "print(s[-1])  # 取最后一位\n",
    "print(s[len(s)-1])\n",
    "\n",
    "print(s[1:])\n",
    "print(s[1:3])\n",
    "print(s[:2])\n",
    "print(s[0:5:2]) #[0.5) 间隔2取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "True\n",
      "True\n",
      "a,b,c,d\n",
      "['a', 'b', 'c', 'd']\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "# 常用字符串处理方法\n",
    "\n",
    "# 统计字符串出现次数\n",
    "print(\"xyzxyzxz\".count(\"xy\"))\n",
    "# 判断字符串全为数字或字母(字符串不为空)\n",
    "print(\"xyzxyzxz\".isalnum())\n",
    "# 判断字符串全为字母(字符串不为空)\n",
    "print(\"xyzxyzxz\".isalpha())\n",
    "\n",
    "print(\",\".join(\"abcd\"))\n",
    "\n",
    "print(\"a,b,c,d\".split(\",\"))\n",
    "\n",
    "print(\"xyz\".startswith(\"x\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
